﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OfficeHelper
{
    public class Utils
    {
        /// <summary>
        /// 得到文本类型文件的内容，默认编码为utf8
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetTextFileContent(string fileName)
        {
            StreamReader sr = new StreamReader(fileName, Encoding.UTF8);
            string content, result = "";
            while ((content = sr.ReadLine()) != null)
            {
                result += content.ToString() + "\r\n";
            }
            return result;
        }

        /// <summary>
        /// 创建文本文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool CreateTextFile(string fileName, string content)
        {
            FileStream fs = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            //开始写入
            sw.Write(content);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
            return true;
        }

        /// <summary>
        /// 得到相对于基文件的文件的完整路径
        /// </summary>
        /// <param name="baseFileName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetRelativeFilePath(string baseFileName, string fileName)
        {
            string path = "";
            if (fileName.IndexOf(":") == -1)
            {
                FileInfo file = new FileInfo(baseFileName);
                path = file.DirectoryName + "/" + fileName;
            }
            else
            {
                path = fileName;
            }
            return path;
        }

        /// <summary>
        /// 将字典的key变为首字母大写的key
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static Dictionary<string, string> FormatDicKeys(Dictionary<string, string> dic)
        {
            // 将所有key变为首字母大写其余字母小写的形式
            Dictionary<string, string> temp = new Dictionary<string, string>();
            foreach (var p in dic)
            {
                string key = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(p.Key);
                temp.Add(key, p.Value);
            }
            return temp;
        }

        /// <summary>
        /// 将字典数据转为对象，注意会强制key的首字母大写
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static T DicToObj<T>(Dictionary<string, string> dic)
        {
            dic = FormatDicKeys(dic);
            string json = JsonConvert.SerializeObject(dic);
            return JsonConvert.DeserializeObject<T>(json);
        }
        
        /// <summary>
        /// 获取MD5加密值
        /// </summary>
        /// <param name="myString"></param>
        /// <returns></returns>
        public static string GetMD5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String.Substring(0, 16);
        }

        /// <summary>
        /// 得到包含日期目录的图片路径
        /// </summary>
        /// <param name="rootPath">根目录</param>
        /// <returns></returns>
        public static string GetPictruePath(string rootPath)
        {
            return GetDatePath(rootPath) + GetRandomName() + ".png";
        }

        /// <summary>
        /// 得到包含日期的路径
        /// </summary>
        /// <param name="rootPath">根目录</param>
        /// <returns></returns>
        public static string GetDatePath(string rootPath)
        {
            DateTime now = DateTime.Now;
            string time_path = now.Year.ToString() + "/" + now.Month.ToString() + "-" + now.Day.ToString() + "/";

            string con_path = rootPath + time_path;
            string full_con_path = AppDomain.CurrentDomain.BaseDirectory + con_path;

            if (!Directory.Exists(full_con_path))
            {
                Directory.CreateDirectory(full_con_path);
            }

            return con_path;
        }

        /// <summary>
        /// 得到随机名
        /// </summary>
        /// <returns></returns>
        public static string GetRandomName()
        {
            return GetMD5(DateTime.Now.Ticks.ToString() + new Random().NextDouble());
        }
    }
}
